package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FragmentUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.stark.file.transfer.core.FileInfo;
import com.stark.file.transfer.core.TfContactInfo;
import com.stark.file.transfer.core.Transferable;
import com.stark.file.transfer.core.TransferableSendManager;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.bean.SelectFileBean;
import flc.ast.databinding.ActivityFileTransferBinding;
import flc.ast.dialog.FileSendShowQrDialog;
import flc.ast.fragment.AudioFragment;
import flc.ast.fragment.PictureFragment;
import flc.ast.fragment.TransferAudioFragment;
import flc.ast.fragment.TransferPicFragment;
import flc.ast.fragment.TransferVideoFragment;
import flc.ast.utils.SendFileManager;
import stark.common.basic.bean.ContactInfo;
import stark.common.basic.constant.FileType;
import stark.common.basic.event.EventStatProxy;

public class FileTransferActivity extends BaseAc<ActivityFileTransferBinding> {

    private List<Fragment> mFragmentList;

    @Override
    protected int onCreate() {
        return R.layout.activity_file_transfer;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivFileTransferBack.setOnClickListener(v -> onBackPressed());

        mFragmentList = new ArrayList<>();
        mFragmentList.add(new TransferPicFragment());
        mFragmentList.add(new TransferVideoFragment());
        mFragmentList.add(new TransferAudioFragment());
        FragmentUtils.add(getSupportFragmentManager(), mFragmentList, R.id.flContainer, 0);

        mDataBinding.tvPicture.setOnClickListener(this);
        mDataBinding.tvVideo.setOnClickListener(this);
        mDataBinding.tvAudio.setOnClickListener(this);
        mDataBinding.tvTransfer.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvPicture://图片
                clearSelection();
                mDataBinding.tvPicture.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvPicture.setTextSize(24);
                mDataBinding.ivPicture.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(0, mFragmentList);
                break;
            case R.id.tvVideo://视频
                clearSelection();
                mDataBinding.tvVideo.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvVideo.setTextSize(24);
                mDataBinding.ivVideo.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(1, mFragmentList);
                break;
            case R.id.tvAudio://音频
                clearSelection();
                mDataBinding.tvAudio.setTextColor(Color.parseColor("#FFFFFF"));
                mDataBinding.tvAudio.setTextSize(24);
                mDataBinding.ivAudio.setVisibility(View.VISIBLE);
                FragmentUtils.showHide(2, mFragmentList);
                break;
            case R.id.tvTransfer://传送
                if (SendFileManager.getInstance().getSelectFileBeans().size() == 0) {
                    ToastUtils.showShort(R.string.no_choose_file_transfer_tips);
                } else {
                    List<Transferable> transferableList = new ArrayList<>();
                    List<ContactInfo> contactInfoList = new ArrayList<>();
                    for (SelectFileBean fileBean : SendFileManager.getInstance().getSelectFileBeans()) {
                        switch (fileBean.getType()) {
                            case 1://图片
                                FileInfo picFile = new FileInfo();
                                picFile.setFileName(fileBean.getName());
                                picFile.setUriStr(fileBean.getPath());
                                picFile.setFileType(FileType.IMAGE);
                                picFile.setSize(fileBean.getSize());
                                transferableList.add(picFile);
                                break;
                            case 2://视频
                                FileInfo videoFile = new FileInfo();
                                videoFile.setFileName(fileBean.getName());
                                videoFile.setUriStr(fileBean.getPath());
                                videoFile.setFileType(FileType.VIDEO);
                                videoFile.setSize(fileBean.getSize());
                                transferableList.add(videoFile);
                                break;
                            case 3://音频
                                FileInfo audioFile = new FileInfo();
                                audioFile.setFileName(fileBean.getName());
                                audioFile.setUriStr(fileBean.getPath());
                                audioFile.setFileType(FileType.AUDIO);
                                audioFile.setSize(fileBean.getSize());
                                transferableList.add(audioFile);
                                break;
                            default:
                                break;
                        }
                    }
                    TransferableSendManager.getInstance().setTransferables(transferableList);
                    FileSendShowQrDialog dialog = new FileSendShowQrDialog(mContext);
                    dialog.show();
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }

    private void clearSelection() {
        mDataBinding.tvPicture.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvPicture.setTextSize(15);
        mDataBinding.ivPicture.setVisibility(View.GONE);
        mDataBinding.tvVideo.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvVideo.setTextSize(15);
        mDataBinding.ivVideo.setVisibility(View.GONE);
        mDataBinding.tvAudio.setTextColor(Color.parseColor("#80FFFFFF"));
        mDataBinding.tvAudio.setTextSize(15);
        mDataBinding.ivAudio.setVisibility(View.GONE);
    }

}